﻿using System;
using System.Collections.Generic;
using System.IO;
using Game;
using Newtonsoft.Json;

namespace YS
{
    public class YSplaceTime
    {
        public bool CheckCoolDown(ComponentPlayer componentPlayer)
        {
            try
            {
                string playerGuid = componentPlayer.PlayerData.PlayerGUID.ToString();
                string playerName = componentPlayer.PlayerData.Name.Replace("\r", "").Replace("\n", "");

                // 构建目录路径
                string directoryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NewMyPlayData", playerGuid);
                if (!Directory.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }

                // 构建文件路径
                string fileName = $"time2{playerName}.json";
                string filePath = Path.Combine(directoryPath, fileName);

                List<DateTime> list;
                if (File.Exists(filePath))
                {
                    using (StreamReader streamReader = new StreamReader(filePath))
                    {
                        list = JsonConvert.DeserializeObject<List<DateTime>>(streamReader.ReadToEnd());
                    }
                }
                else
                {
                    list = new List<DateTime> { DateTime.Now };
                    string contents = JsonConvert.SerializeObject(list);
                    File.WriteAllText(filePath, contents);
                }

                DateTime now = DateTime.Now;
                foreach (DateTime item in list)
                {
                    if ((now - item).TotalSeconds > 30.0)
                    {
                        return true;
                    }
                }

                return false;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"检查冷却时出错: {ex.Message}");
                return false;
            }
        }

        public void ResetCoolDown(ComponentPlayer componentPlayer)
        {
            try
            {
                string playerGuid = componentPlayer.PlayerData.PlayerGUID.ToString();
                string playerName = componentPlayer.PlayerData.Name.Replace("\r", "").Replace("\n", "");

                // 构建目录路径
                string directoryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NewMyPlayData", playerGuid);
                if (!Directory.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }

                // 构建文件路径
                string fileName = $"time2{playerName}.json";
                string filePath = Path.Combine(directoryPath, fileName);

                string contents = JsonConvert.SerializeObject(new List<DateTime> { DateTime.Now });
                File.WriteAllText(filePath, contents);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"检查冷却时出错: {ex.Message}");
            }
        }
    }
}